﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.EFCore.Respository;
using XsqTech.SmartOA.IRespository;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Service
{
    /// <summary>
    /// 权限
    /// </summary>
    public class PermissionService : IPermissionService
    {
        IPermissionRespository _basePermission;
        readonly IMapper _mapper;
        readonly IUnitOfWork _unitOfWork;
        private readonly ILogger<MenuService> _logger;

        public PermissionService(IPermissionRespository basePermission, IMapper mapper, IUnitOfWork unitOfWork, ILogger<MenuService> logger)
        {
            _basePermission = basePermission;
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _logger = logger;
        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <returns></returns>
        public dynamic Recursion()
        {
            var id = Guid.Empty;
            var m = RecursionChildren(id);
            return new ResponseModel
            {
                Data = m,
                Message = "请求成功",
            };
        }
        /// <summary>
        /// 递归子级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<PermissionDto> RecursionChildren(Guid id)
        {
            var list = _basePermission.Query().Where(x => x.ParentId.Equals(id)).ToList();
            List<PermissionDto> Children = new List<PermissionDto>();

            foreach (var item in list)
            {
                if (item.IsMenu==true)
                {
                    Children.Add(new PermissionDto
                    {
                        ParentId = item.ParentId,
                        IconName = item.IconName,
                        IsMenu = item.IsMenu,
                        PId = item.PId,
                        PTitle = item.PTitle,
                        PAction = item.PAction,
                        PApiUrl = item.PApiUrl,
                        children = RecursionChildren(item.PId),
                    });
                }
            }

            return Children;

        }
        /// <summary>
        /// 权限添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public ResponseModel PermissionAdd(PermissionDto m)
        {
            var s = _mapper.Map<Permission>(m);
            _basePermission.Add(s);
            int data = _unitOfWork.SaveChangs();
            if (data > 0)
            {
                return new ResponseModel
                {
                    Code = State.请求成功,
                    Message = "添加成功",
                    Count = data,
                };
            }
            else
            {
                return new ResponseModel
                {
                    Code = State.请求失败,
                    Message = "添加失败",
                    Count = data,
                };
            }
        }
        /// <summary>
        /// 权限删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel PermissionDel(string id)
        {
            var list = _basePermission.Query().FirstOrDefault(x => x.PId.ToString().Equals(id));
            list.IsMenu=false;
            _basePermission.Update(list);
            //_basePermission.Delete(list.PId);
            int data = _unitOfWork.SaveChangs();
            if (data > 0)
            {
                return new ResponseModel
                {
                    Code = State.请求成功,
                    Message = "删除成功",
                    Count = data,
                };
            }
            else
            {
                return new ResponseModel
                {
                    Code = State.请求失败,
                    Message = "删除失败",
                    Count = data,
                };
            }
        }
        /// <summary>
        /// 权限修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ResponseModel PermissionUpt(PermissionDto dto)
        {
            try
            {
                var s = _mapper.Map<Permission>(dto);
                _basePermission.Update(s);
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "修改成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "修改失败",
                        Count = data,
                    };
                }
            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };

        }
        /// <summary>
        /// 根据主键获取权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel PermissionGet(string id)
        {
            var list = _basePermission.Query().FirstOrDefault(x => x.PId.ToString().Equals(id));
            return new ResponseModel
            {
                Code = State.请求成功,
                Message = "请求成功",
                Data = list,
            };
        }
    }
}
